﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.IO;
using System.Reflection;

namespace UFrameWork
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "./Config/Main.config");
            AppDomain.CurrentDomain.UnhandledException += UnhandledException;

            try
            {
                LoadAssembly();
                UWork.Do(UEvent.INIT_FINISH);
                ConsoleCmd.Register("quit", Quit);
                ConsoleCmd.Handle();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.ReadKey();
            }
        }

        /// <summary>
        /// 全局异常捕获
        /// </summary>
        static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine(e.ExceptionObject);
        }

        /// <summary>
        /// 加载dll
        /// </summary>
        static void LoadAssembly()
        {
            string[] dlls = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.dll");

            foreach (string dll in dlls)
            {
                UWork.RegAssembly(Assembly.UnsafeLoadFrom(dll));
            }
        }

        static void Quit(string[] data)
        {
            bool quit = true;
            object ret = UWork.Do<object>(UEvent.SERVER_QUIT);
            if (ret != null) quit = (bool)ret;
            if (quit) Environment.Exit(0);
        }
    }
}